<template>
  <div class ="news_classification">
    <!-- 按钮开始 -->
        <el-button @click="showHandler" size='small' type="primary">新增</el-button>
    <!-- 按钮结束 -->
    <!-- 表格开始 -->
        <el-table
        :data="news_classification"
        style="width: 100%">
        <el-table-column
            prop="id"
            label="序号"
            width="90"
            align='center'>
        </el-table-column>
        <el-table-column
            prop="name" label="名称" align='center'>
        </el-table-column>
        <el-table-column 
            prop="no" label="排序号" align='center'>
        </el-table-column>
        <el-table-column 
            prop="description" label="描述" align='center'>
        </el-table-column>
        <el-table-column width='160px' label="操作" align='center'>
            <template slot-scope="scope">
                <el-button @click='editHandler(scope.row)' size="mini" type="primary">编辑</el-button>
                <el-button @click="deleteHandler(scope.row.id)" size="mini" type="danger">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
    <!-- 表格结束 -->
   
    <!-- 模态框开始 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <!-- 表单开始 -->
        <!-- {{ruleForm}} -->
        <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="栏目名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="排序号" prop="no">
            <el-input v-model="ruleForm.no"></el-input>
          </el-form-item>
          <el-form-item label="栏目介绍" prop="desicription">
            <el-input v-model="ruleForm.description"></el-input>
          </el-form-item>
        </el-form>
        <!-- 表单结束 -->
        <div slot="footer" class="dialog-footer">
          <el-button size='mini' @click="dialogFormVisible = false">取 消</el-button>
          <el-button size='mini' type="primary" @click="submitHandler">确 定</el-button>
        </div>
      </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import {mapState,mapActions} from 'vuex'
export default {
  data(){
    return {
      dialogFormVisible:false,
      ruleForm:{},
      title:'新增栏目信息'
    }
  },
  computed:{
    ...mapState('news_classification',['news_classification'])
  },
  methods:{
    ...mapActions('news_classification',['findAllNews','saveNews','deleteNews']),
    
    //去添加栏目信息
    showHandler(){
      this.dialogFormVisible = true
    },
    //提交保存
    submitHandler(){
      this.saveNews(this.ruleForm).then(res =>{
        console.log(res);
        if(res.status === 200){
          this.dialogFormVisible = false
          this.findAllNews(this.news_classification)
          this.$message({
            type: 'success',
            message:res.message
          })
        }else {
          this.$message({
            type: 'error',
            message:res.message
          })
        }
      })
    },
    //编辑栏目信息
    editHandler(data){
      // 将当前行的内容，赋值给我们的ruleForm
        this.ruleForm = data
        this.title = '编辑栏目信息'
      //打开模态框
        this.dialogFormVisible = true
    },
    //删除栏目信息
      deleteHandler(id){
        let params = {
          id
        }
        this.deleteNews(params).then(res =>{
          if(res.status === 200){
            this.findAllNews(this.news_classification)
            this.$message({
              type:'success',
              message: res.message
            })
          }else {
              this.$message({
              type:'error',
              message: res.message
            })
          }
        })
      },
      
  },
  created(){
    this.findAllNews(this.news_classification)
  }
}
</script>
<style scoped>

</style>
